<template>
  <div>
    <basic-container>
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>任务管理</el-breadcrumb-item>
        <el-breadcrumb-item>阅读时长领书币</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud
        :table-loading="loading"
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :defaults.sync="defaults"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @row-update="rowUpdate"
        @row-del="rowDel"
        @row-save="saveList"
      >
        <template slot-scope="scope" slot="author_idsForm">
          <div class="authorId">
            <el-input
              v-model="form.author_ids"
              placeholder="请输入作者ID,多个作者以英文逗号隔开"
            ></el-input>
            <el-button type="primary" size="small" @click.native="getAuthorName"
              >查询</el-button
            >
          </div>
        </template>
        <template slot-scope="scope" slot="last_operation_info">
          <div class="authorId">
            {{ scope.row.edit_time }}
            <el-tag>{{ scope.row.edit_admin_name }}</el-tag>
          </div>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import {
  pcConfigMessagePush,
  appConfigReadReward,
  selectVersion,
} from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      radio: "1",
      resData: "",
      userInfo: this.$store.state.user.userInfo,
      defaults: {},
      loading: false,
      page: {
        total: 0,
        pageSize: 10,
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        orderByList: "edit_time&DESC",
      },
      option: {
        index: true,
        indexLabel: "序号",
        border: true,
        column: [
          {
            label: "版本号",
            prop: "app_version",
            type: "select",
            span: 24,
            row: true,
            editDisplay: false,
            dicUrl: `${catUrl + selectVersion.selectVersion}`,
            dicFormatter: (res) => {
              return res.source;
            },
            props: {
              label: "app_version",
              value: "app_version",
            },
          },
          {
            label: "排序",
            prop: "sort",
            type: "select",
            span: 24,
            // formslot: true,
            row: true,
            dicData: [
              { label: "1", value: 1 },
              { label: "2", value: 2 },
              { label: "3", value: 3 },
              { label: "4", value: 4 },
              { label: "5", value: 5 },
              { label: "6", value: 6 },
            ],
          },
          {
            label: "奖励类型",
            prop: "reward_type",
            type: "radio",
            span: 24,
            // formslot: true,
            row: true,
            dicData: [
              { label: "书币", value: 1 },
              { label: ".鱼摆摆", value: 2 },
            ],
          },
          {
            label: "阅读时长",
            prop: "read_time",
            // formslot: true,
            type: "input",
            row: true,
            span: 12,
            rules: [
              {
                required: true,
                message: "作者ID必须填写",
              },
            ],
            search: false,
            addDisplay: true,
            editDisplay: true,
            addDisabled: false,
            editDisabled: false,
          },
          {
            label: "奖励数量",
            prop: "reward_count",
            // formslot: true,
            type: "input",
            row: true,
            span: 12,
            search: false,
          },
          {
            label: "最后操作信息",
            prop: "last_operation_info",
            type: "input",
            slot: true,
            addDisplay: false,
            editDisplay: false,
            addDisabled: true,
            editDisabled: true,
          },
        ],
      },
    };
  },
  methods: {
    //获取作者名字
    // async getAuthorName() {
    //   let params = { authorIdList: this.form.author_ids.split(",") };
    //   let response = await common(
    //     getAuthorNameByAuthorIds.getAuthorNameByAuthorIds,
    //     params,
    //     {},
    //     "POST"
    //   );
    //   console.log(response, "respones");
    //   if (response.code == 1) {
    //     this.resData = response.data.join(",");
    //   }
    // },
    async saveList(row, done, loading) {
      let params = {
        app_version: this.form.app_version,
        edit_admin_id: this.userInfo.id,
        edit_admin_name: this.userInfo.username,
        read_time: this.form.read_time,
        reward_count: this.form.reward_count,
        reward_type: this.form.reward_type,
        sort: this.form.sort,
      };
      let response = await common(appConfigReadReward.save, params, {}, "POST");
      if (response.code == 1) {
        done();
        this.getList();
      }
      loading();
    },
    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        appConfigReadReward.pageAndParam,
        { ...this.default, ...this.params },
        this.page,
        "POST"
      );
      this.data = response.data;
      this.loading = false;
    },
    /**保存 */
    async rowSave(row, done, loading) {
      delete row["id"];
      let userInfo = this.$store.state.user.userInfo;
      row.edit_admin_id = userInfo.id;
      row.edit_admin_name = userInfo.username;
      let data = await common(pcConfigMessagePush.save, row, null, "POST");
      if (data == null) {
        loading();
        return;
      }
      this.getList();
      done();
    },
    /**修改 */
    async rowUpdate(row, index, done, loading) {
      let response = await common(
        appConfigReadReward.update,
        this.form,
        {},
        "POST"
      );
      if (response.code == 1) {
        this.getList();
        done();
      }
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**删除 */
    rowDel(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        type: "warning",
      })
        .then(async () => {
          let response = await common(
            appConfigReadReward.delete,
            { id: row.id },
            null,
            "GET"
          );
        })
        .then(() => {
          this.getList();
        });
    },
    /**刷新 */
    refreshChange() {
      this.getList();
    },
    /**搜索 */
    searchChange(params, done) {
      console.log(params, "params");
      params.push_time = `${params.push_time ? "!<>!" + params.push_time : ""}`;
      // params.edit_account =  '%'+params.edit_account+'%' //模糊搜索
      delete params.push_time;
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
  mounted() {},
};
</script>
<style scoped>
.authorId {
  display: flex;
}
.nameTitle {
  width: 120px;
}
</style>